<template>
  <div>
    <!-- 搜索内容 -->
    <div class="search-header">
      <van-icon
        name="arrow-left"
        color="white"
        size="0.48rem"
        class="goback"
        @click="$router.back()"
      ></van-icon>
      <van-search
        placeholder="请输入搜索关键词"
        background="#007bff"
        shape="round"
        v-focus
        @input="inputEv"
        v-model.trim="kw"
        @search="searchEv"
      ></van-search>
    </div>
    <!-- 搜索结果展示 -->
    <div class="searchList" v-if="kw.length !== 0">
      <div
        class="searchItem"
        v-for="(item, index) in sugList"
        :key="index"
        v-html="initFont(item, kw)"
        @click="suggetEv(item)"
      ></div>
    </div>
    <!-- 搜索历史记录 -->
    <div class="searchHistory" v-else>
      <van-cell title="搜索历史">
        <template #right-icon>
          <van-icon name="delete" class="searchIcon" @click="deleteHistory"></van-icon>
        </template>
      </van-cell>
      <div class="historyList">
        <span
          class="historyItem"
          v-for="(item, index) in history"
          @click="historyEv(item)"
          >{{ item }}</span
        >
      </div>
    </div>
  </div>
</template>

<script>
import { searchSuggestion } from "@/api/index";
import { Toast } from "vant";
export default {
  name: "search",
  data() {
    return {
      kw: "",
      timer: "",
      sugList: [],
      history: JSON.parse(localStorage.getItem("history")) || [],
    };
  },
  methods: {
    inputEv() {
      clearTimeout(this.timer);
      if (this.kw.length === 0) {
        this.sugList = [];
      } else {
        this.timer = setTimeout(async () => {
          let res = await searchSuggestion(this.kw);
          this.sugList = res.data.data.options;
        }, 300);
      }
    },
    initFont(objSelf, target) {
      if (objSelf == null) {
        Toast.fail("暂无搜索内容");
      } else {
        const reg = new RegExp(target, "ig");
        return objSelf.replace(reg, (match) => {
          return `<span style="color:red;">${match}</span>`;
        });
      }
    },
    // 封装跳转函数
    moveRes(e) {
      setTimeout(() => {
        this.$router.push({
          path: `/searchresult/${e}`,
        });
      }, 0);
    },
    searchEv() {
      if (this.kw.length > 0) {
        this.history.push(this.kw);
        this.moveRes(this.kw);
      }
    },
    suggetEv(val) {
      this.history.push(val);
      this.moveRes(val);
    },
    historyEv(val) {
      this.moveRes(val);
    },
    deleteHistory(){
        this.history=[]
    }
  },
  watch: {
    history: {
      handler() {
        let res = Array.from(new Set(this.history));
        localStorage.setItem("history", JSON.stringify(res));
      },
      deep: true,
    },
  },
};
</script>

<style scoped lang="less">
.search-header {
  height: 46px;
  display: flex;
  align-items: center;
  background-color: #007bff;
  overflow: hidden;
  .goback {
    padding-left: 14px;
  }
  .van-search {
    flex: 1;
  }
}
.searchList {
  .searchItem {
    padding: 0 15px;
    border-bottom: 1px solid #f8f8f8;
    font-size: 14px;
    line-height: 50px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
.searchIcon {
  font-size: 16px;
  line-height: inherit;
}
.historyList {
  padding: 0 10px;
  .historyItem {
    display: inline-block;
    font-size: 12px;
    padding: 8px 14px;
    background-color: #efefef;
    margin: 10px 8px 0px 8px;
    border-radius: 10px;
  }
}
</style>